import db from '../../db.js';
/**
 *
 * @param {array} data
 * @returns Boolean
 */
const save = async (data) => {
  const [result] = await db.query(
    'INSERT INTO `role`(`roleName`,`create_time`,`update_time`,`roleCode`,`description`) VALUES (?,?,?,?,?)',
    data,
  );
  if (result?.affectedRows > 0) {
    return true;
  }
  return false;
};
/**
 *
 * @returns array
 */
const getList = async () => {
  const [row] = await db.query(
    'select id,roleName,roleCode,description,create_time as createTime,update_time as updateTime from `role` where isDelete=0',
  );
  return row || [];
};
const findById = async (id) => {
  const [row] = await db.query(
    'select id,roleName,roleCode,description,create_time as createTime,update_time as updateTime from `role` where isDelete=0 and id=? limit 1',
    id,
  );
  return row || [];
};
const del = async (roleId) => {
  const [result] = await db.query('update   `role` set isDelete=1 where id=?', roleId);
  if (result?.affectedRows > 0) {
    return true;
  }
  return false;
};
const update = async (updateData, roleId) => {
  const [result] = await db.query('update `role` set?  where id=?', [updateData, roleId]);
  if (result?.affectedRows > 0) {
    return true;
  }
  return false;
};
export default {
  save,
  getList,
  del,
  findById,
  update,
};
